// grammar.pegjs
Start = _ statements:Statement+ _ { return statements; }

Statement
  = VariableDecl
  / IfStatement
  / FunctionCall

// 变量声明：变量 类型 = 值;
VariableDecl
  = _ name:Identifier _ "是" _ type:("整数" / "文本") _ "=" _ value:(Number / String) _ ";" {
      return { type: "VariableDecl", name, dataType: type, value };
  }

// if语句：如果 (条件) 则 语句;
IfStatement
  = _ "如果" _ "(" _ condition:Condition _ ")" _ "则" _ statement:Statement _ {
      return { type: "IfStatement", condition, thenBranch: statement };
  }

// 函数调用：函数名(参数);
FunctionCall
  = _ name:Identifier _ "(" _ args:ArgumentList? _ ")" _ ";" {
      return { type: "FunctionCall", name, args: args || [] };
  }

// 条件表达式：变量 > 值
Condition
  = left:Identifier _ op:(">"/"<") _ right:Number {
      return { type: "BinaryOp", left, op, right };
  }

// 参数列表：值, 值
ArgumentList
  = first:Expression _ rest:(_ "," _ Expression)* {
      return [first].concat(rest.flatMap(([_, __, ___, arg]) => arg));
  }

// 基础表达式
Expression
  = Number
  / String
  / Identifier

// 数字：123
Number = [0-9]+ { return parseInt(text(), 10); }

// 标识符：abc
Identifier = [a-zA-Z_\u4e00-\u9fa5]+ { return text(); }

// 字符串："abc"
String = "\"" [^"]* "\"" { return text().slice(1, -1); }

// 空白字符（忽略）
Comment = "//" [^\n\r]*
_ = (Comment / [ \t\n\r])*